package com.vogue.supplymall.order.controller;

import com.vogue.supplymall.common.domain.dto.ControllerResultInfo;
import com.vogue.supplymall.common.domain.dto.SearchOrderInfo;
import com.vogue.supplymall.common.domain.dto.UserInfo;
import com.vogue.supplymall.common.domain.tool.MessageSourceUtils;
import com.vogue.supplymall.order.domain.biz.OrderListBiz;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * The type Cart controller.
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-01-09
 */
@RestController
public class OrderListController extends BaseController {
    /**
     * The constant log.
     */
    public static final Logger log = LoggerFactory.getLogger(OrderListController.class);

    @Resource
    private OrderListBiz orderListBiz;
    @Resource
    private MessageSourceUtils messageSourceUtils;

    /**
     * Gets cart.
     * 获取当前用户的购物车
     *
     * @param appver the appver
     * @return the cart
     */
    @RequestMapping(value = "/is/{appver}/order/buyer/orderList", method = RequestMethod.POST)
    public ResponseEntity<Object> getOrderList(@PathVariable("appver") String appver, @RequestBody(required = false) SearchOrderInfo searchOrderInfo) {
        log.info("get order list ");
        if (searchOrderInfo == null) {
            searchOrderInfo = new SearchOrderInfo();

        }
//        searchOrderInfo.setIfBuyer(true);
        UserInfo info = new UserInfo();
        if (SecurityUtils.getSubject().isAuthenticated()) {
            info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            searchOrderInfo.setLoginId(info.getLoginId());
            searchOrderInfo.setIsAdmin(info.isAdmin());
            if (null != info.getOrganInfo()) {
                searchOrderInfo.setOrganid(info.getOrganInfo().getId());
            } else {
                searchOrderInfo.setOrganid(info.getLoginId());
            }
            searchOrderInfo.setSyscodeid(info.getSyscodeid());
        } else {
            log.info("获取登信息失败！");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setData(orderListBiz.findOrderList(searchOrderInfo,info));
        log.info("get order list end ");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    @RequestMapping(value = "/is/{appver}/order/buyer/orderCount", method = RequestMethod.POST)
    public ResponseEntity<Object> getOrderCount(@PathVariable("appver") String appver, @RequestBody(required = false) SearchOrderInfo searchOrderInfo) {
        log.info("get order count ");
        if (searchOrderInfo == null) {
            searchOrderInfo = new SearchOrderInfo();

        }

        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            searchOrderInfo.setLoginId(info.getLoginId());
            searchOrderInfo.setIsAdmin(info.isAdmin());
            if (null != info.getOrganInfo()) {
                searchOrderInfo.setOrganid(info.getOrganInfo().getId());
            } else {
                searchOrderInfo.setOrganid(info.getLoginId());
            }
            searchOrderInfo.setSyscodeid(info.getSyscodeid());
        } else {
            log.info("获取登信息失败！");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setData(orderListBiz.findOrderCount(searchOrderInfo));
        log.info("get order count end ");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

}
